package com.info.zhiduoduo.admin.service;

import com.baomidou.mybatisplus.mapper.Condition;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.info.zhiduoduo.admin.model.formbean.evaluate.QueryEvaluateByConditionFormBean;
import com.info.zhiduoduo.admin.model.formbean.evaluate.UpdateEvaluateDateFormBean;
import com.info.zhiduoduo.admin.model.formbean.evaluate.UpdateEvaluateFormBean;
import com.info.zhiduoduo.common.constants.SmartResult;
import com.info.zhiduoduo.common.constants.system.UserType;
import com.info.zhiduoduo.common.exception.Asserts;
import com.info.zhiduoduo.common.formbean.BaseSimpleFormBean;
import com.info.zhiduoduo.common.utils.DateUtil;
import com.info.zhiduoduo.core.service.common.BaseService;
import com.info.zhiduoduo.repository.dto.evaluate.EvaluateListDto;
import com.info.zhiduoduo.repository.entity.company.Company;
import com.info.zhiduoduo.repository.entity.crowdsource.CrowdsourceAccount;
import com.info.zhiduoduo.repository.entity.evaluate.Evaluate;
import com.info.zhiduoduo.repository.entity.maker.MakerAccount;
import com.info.zhiduoduo.repository.entity.maker.MakerStudio;
import com.info.zhiduoduo.repository.entity.partner.PartnerAccount;
import com.info.zhiduoduo.repository.mapper.evaluate.EvaluateMapper;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

@Transactional
@Service
public class EvaluateService extends BaseService {

	@Resource
	EvaluateMapper evaluateMapper;

	public SmartResult queryEvaluate(BaseSimpleFormBean<QueryEvaluateByConditionFormBean> formBean){
		QueryEvaluateByConditionFormBean reqdata = formBean.getReqdata();
		if(null == reqdata){
			return errorParamSmartResult();
		}
		//开始日期
		Date startDate = DateUtil.defaultInitial(reqdata.getStartDate());
		//结束日期
		Date endDate = DateUtil.defaultNow(reqdata.getEndDate());

		int projectType = reqdata.getProjectType();
		int userType = reqdata.getUserType();
		String platformGrade = reqdata.getPlatformGrade();
		String appraiseeGrade = reqdata.getAppraiseeGrade();
		String whetherRecommend = reqdata.getWhetherRecommend();
		Integer type = reqdata.getType();

		String userTypes = "0";
		if (userType == 1) {
			userTypes = StringUtils.joinWith(",", UserType.MAKER, UserType.MAKER_CROWDSOURCE, UserType.TAXER);
		} else if (userType == 2) {
			userTypes = String.valueOf(UserType.COMPANY);
		} else if (userType == 3) {
			userTypes = String.valueOf(UserType.CROWDSOURCE);
		} else if (userType == 0) {
			userTypes = StringUtils.joinWith(",", UserType.MAKER, UserType.MAKER_CROWDSOURCE, UserType.TAXER, UserType.COMPANY, UserType.CROWDSOURCE);
		}

		Page<Object> page = PageHelper.startPage(formBean.getPage(), formBean.getRows());
		List<EvaluateListDto> evaluateListDtoList = evaluateMapper.queryEvaluateByCondition(projectType, startDate,endDate,
				DateUtil.getPreviousWorkingDay(new Date()),userTypes,
				platformGrade,appraiseeGrade,whetherRecommend,type);
		return structureData(evaluateListDtoList,page);
	}

	private SmartResult structureData(List<EvaluateListDto> evaluateListDtoList, Page<Object> page){
		evaluateListDtoList.forEach(dto -> {
			// 评价方用户类型：1. 创客，2.企业，3.合伙人，4.众包自然人，5. 创客 6.众包创客
			int evaluatorUserType = dto.getEvaluatorUserType();
			int evaluatorUserId = dto.getEvaluatorUserId();
			if(1 == evaluatorUserType){
				//获取创客姓名
				MakerAccount account = new MakerAccount().selectOne(Condition.create().eq("userid", evaluatorUserId));
				if(null != account){
					dto.setEvaluatorUserName(account.getRealname());
				}
				//获取创客工作室
				MakerStudio makerStudio =new MakerStudio().selectOne(Condition.create().eq("userid", evaluatorUserId));
				if (null !=makerStudio) {
					dto.setUserName(makerStudio.getUsername());
				}
			} else if (2 == evaluatorUserType) {
				//获取企业名称
				Company company = new Company().selectById(evaluatorUserId);
				if(null != company){
					dto.setEvaluatorUserName(company.getCompname());
				}
			} else if (3 == evaluatorUserType) {
				PartnerAccount partnerAccount = new PartnerAccount().selectOne(Condition.create().eq("partnerid", evaluatorUserId));
				if (partnerAccount != null) {
					dto.setEvaluatorUserName(partnerAccount.getRealname());
				}
			} else if (4 == evaluatorUserType) {
				CrowdsourceAccount crowdsourceAccount = new CrowdsourceAccount().selectOne(
						Condition.create().eq("crowdsourceid", evaluatorUserId));
				if (crowdsourceAccount != null) {
					dto.setEvaluatorUserName(crowdsourceAccount.getRealname());
				}
			} else if (5 == evaluatorUserType) {
				//获取 创客姓名
				MakerAccount account = new MakerAccount().selectOne(Condition.create().eq("userid", evaluatorUserId));
				if(null != account){
					dto.setEvaluatorUserName(account.getRealname());
				}
				//获取创客工作室
				MakerStudio makerStudio =new MakerStudio().selectOne(Condition.create().eq("userid", evaluatorUserId));
				if (null !=makerStudio) {
					dto.setUserName(makerStudio.getUsername());
				}
			} else if (6 == evaluatorUserType) {
				//获取众包创客姓名
				MakerAccount account = new MakerAccount().selectOne(Condition.create().eq("userid", evaluatorUserId));
				if (null != account) {
					dto.setEvaluatorUserName(account.getRealname());
				}
				//获取创客工作室
				MakerStudio makerStudio =new MakerStudio().selectOne(Condition.create().eq("userid", evaluatorUserId));
				if (null !=makerStudio) {
					dto.setUserName(makerStudio.getUsername());
				}
			}
			// 被评价方用户类型：1. 创客，2.企业，3.合伙人，4.众包自然人，5. 创客 6.众包创客
			int appraiseeUserType = dto.getAppraiseeUserType();
			int appraiseeUserId = dto.getAppraiseeUserId();
			if(1 == appraiseeUserType){
				//获取创客姓名
				MakerAccount account = new MakerAccount().selectOne(Condition.create().eq("userid", appraiseeUserId));
				if(null != account){
					dto.setAppraiseeUserName(account.getRealname());
				}
				//获取创客工作室
				MakerStudio makerStudio =new MakerStudio().selectOne(Condition.create().eq("userid", appraiseeUserId));
				if (null !=makerStudio) {
					dto.setUserName(makerStudio.getUsername());
				}
			} else if (2 == appraiseeUserType) {
				//获取企业名称
				Company company = new Company().selectById(appraiseeUserId);
				if(null != company){
					dto.setAppraiseeUserName(company.getCompname());
				}
			} else if (3 == appraiseeUserType) {
				PartnerAccount partnerAccount = new PartnerAccount().selectOne(Condition.create().eq("partnerid", appraiseeUserId));
				if (partnerAccount != null) {
					dto.setAppraiseeUserName(partnerAccount.getRealname());
				}
			} else if (4 == appraiseeUserType) {
				CrowdsourceAccount crowdsourceAccount = new CrowdsourceAccount().selectOne(
						Condition.create().eq("crowdsourceid", appraiseeUserId));
				if (crowdsourceAccount != null) {
					dto.setAppraiseeUserName(crowdsourceAccount.getRealname());
				}
			} else if (5 == appraiseeUserType) {
				//获取 创客姓名
				MakerAccount account = new MakerAccount().selectOne(Condition.create().eq("userid", appraiseeUserId));
				if(null != account){
					dto.setAppraiseeUserName(account.getRealname());
				}
				//获取创客工作室
				MakerStudio makerStudio =new MakerStudio().selectOne(Condition.create().eq("userid", appraiseeUserId));
				if (null !=makerStudio) {
					dto.setUserName(makerStudio.getUsername());
				}
			} else if (6 == appraiseeUserType) {
				//获取众包创客姓名
				MakerAccount account = new MakerAccount().selectOne(Condition.create().eq("userid", appraiseeUserId));
				if(null != account){
					dto.setAppraiseeUserName(account.getRealname());
				}
				//获取创客工作室
				MakerStudio makerStudio =new MakerStudio().selectOne(Condition.create().eq("userid", appraiseeUserId));
				if (null !=makerStudio) {
					dto.setUserName(makerStudio.getUsername());
				}
			}
		});
		return smartResult(evaluateListDtoList, page.getTotal(), page.getPages(), page.getPageNum());
	}

	public SmartResult updateEvaluate(BaseSimpleFormBean<UpdateEvaluateFormBean> formBean) {
		if (null == formBean || null == formBean.getReqdata()) {
			return errorParamSmartResult();
		}
		UpdateEvaluateFormBean reqdata = formBean.getReqdata();
		Evaluate evaluate = new Evaluate().selectById(reqdata.getId());
		Asserts.notNull(evaluate);
		evaluate.setFollowExplain(reqdata.getFollowExplain()).updateById();
		return smartResult();
	}

	/**
	 * 设置跟进日期
	 * @param formBean
	 * @return
	 */
	public SmartResult updateEvaluateDate( BaseSimpleFormBean<UpdateEvaluateDateFormBean> formBean) {

		if (null == formBean || null == formBean.getReqdata()) {
			return errorParamSmartResult();
		}
		UpdateEvaluateDateFormBean reqdata = formBean.getReqdata();
		Evaluate evaluate = new Evaluate().selectById(reqdata.getId());
		Asserts.notNull(evaluate);
		evaluateMapper.updateEvaluateDate(reqdata.getId(),reqdata.getFollowDate());
		return smartResult();
	}
}
